package com.itmg.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.itmg.dao.KeywordsDAO;
import com.itmg.dao.bean.ConditionBean;
import com.itmg.dao.model.KeywordsModel;
import com.itmg.util.Constants;

public class KeywordsDAOImpl extends SqlMapClientDaoSupport implements KeywordsDAO {

	@Override
	public List<KeywordsModel> getTopKeywords() throws Exception {
		List<KeywordsModel> list = new ArrayList<KeywordsModel>();
		
		String sql = "select * from shop_keywords";
		Connection con = null;
		try{
			con = getSqlMapClientTemplate().getDataSource().getConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				KeywordsModel model = new KeywordsModel();
				model.setId(rs.getInt("id"));
				model.setKeywords(rs.getString("keywords"));
				model.setIsindex(rs.getInt("isindex"));
				list.add(model);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	//插入关键字和首页的图片信息
	public void saveKeywordsAndImage(ConditionBean conditionBean) throws Exception{
		String sql = "";
		PreparedStatement ps = null;
		Connection con = null;
		try{
			con = getSqlMapClientTemplate().getDataSource().getConnection();
			if(conditionBean.getSearchResultItemList().size() != 0){
				if(!con.createStatement().executeQuery("select keywords from shop_keywords where keywords = '"+conditionBean.getKeywords()+"'").next()){
					if(conditionBean.getKeywords() != null && !conditionBean.getKeywords().matches(Constants.REGEX_NUMBER) && conditionBean.getKeywords().length()>1){
						 sql = "insert into shop_keywords (keywords,create_time) values (?,?)";
						 ps = con.prepareStatement(sql);
						 ps.setString(1, conditionBean.getKeywords().trim());
						 ps.setTimestamp(2, conditionBean.getCreate_time());
						 ps.executeUpdate();
					}
				}
			}
			 
			//插入图片信息
			if(Constants.SORT_PRICE_UP_TO_DOWN.equals(conditionBean.getSort())){
				if(!con.createStatement().executeQuery("select name from shop_top where name = '"+conditionBean.getKeywords()+"'").next()){
					sql = "insert into shop_top (name,content,detail_url,image_url,create_time) values (?,?,?,?,?)";
					if(conditionBean.getSearchResultItemList() != null && conditionBean.getSearchResultItemList().size() != 0){
						String keywords = conditionBean.getKeywords();
						int count = 0;
						if(keywords.matches(Constants.REGEX_CHARS))
							count = 11;
						else
							count = 7;
						if(keywords != null && keywords.length()<count){
							ps = con.prepareStatement(sql);
							ps.setString(1,conditionBean.getKeywords().trim());
							ps.setString(2,conditionBean.getSearchResultItemList().get(0).getComment());
							ps.setString(3,conditionBean.getSearchResultItemList().get(0).getProductUrl());
							ps.setString(4,conditionBean.getSearchResultItemList().get(0).getSmallImageUrl());
							ps.setTimestamp(5, conditionBean.getCreate_time());
							ps.executeUpdate();
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			con.close();
		}
	}

}
